package com.firebird.app.kotlin.stock.ui.allocation_manage.presenter

import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.AllocationManageBean
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.firebird.app.kotlin.stock.App
import com.firebird.app.kotlin.stock.ui.allocation_manage.contract.AllocationManageContract
import com.firebird.app.kotlin.stock.ui.allocation_manage.model.AllocationManageModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class AllocationManagePresenter : BasePresenter<AllocationManageContract.View?>(), Presenter {
    private val model: AllocationManageContract.Model
    fun getData(bpStatus: String?, bpType: String?, currentPage: String?, itemsPerPage: String?) {
        if (!isViewAttached) {
            return
        }
        mView.showLoading()
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(object : Callback<AllocationManageBean?> {
            override fun onResponse(call: Call<AllocationManageBean?>, response: Response<AllocationManageBean?>) {
                mView.hideLoading()
                mView.onSuccess(response.body())
                SharedPreferencesUtil.getInstance(App.context)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<AllocationManageBean?>, t: Throwable) {
                mView.hideLoading()
                mView.onError(t)
            }
        })
    }

    fun getDataFresh(bpStatus: String?, bpType: String?, currentPage: String?, itemsPerPage: String?) {
        if (!isViewAttached) {
            return
        }
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(object : Callback<AllocationManageBean?> {
            override fun onResponse(call: Call<AllocationManageBean?>, response: Response<AllocationManageBean?>) {
                mView.onSuccessFresh(response.body())
                SharedPreferencesUtil.getInstance(App.context)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<AllocationManageBean?>, t: Throwable) {
                mView.onError(t)
            }
        })
    }

    fun getDataLoadMore(bpStatus: String?, bpType: String?, currentPage: String?, itemsPerPage: String?) {
        if (!isViewAttached) {
            return
        }
        model.getData(bpStatus, bpType, currentPage, itemsPerPage).enqueue(object : Callback<AllocationManageBean?> {
            override fun onResponse(call: Call<AllocationManageBean?>, response: Response<AllocationManageBean?>) {
                mView.onSuccessLoadMore(response.body())
                SharedPreferencesUtil.getInstance(App.context)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<AllocationManageBean?>, t: Throwable) {
                mView.onError(t)
            }
        })
    }

    init {
        model = AllocationManageModel()
    }
}